package com.xxgc.mq.controller;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/mq")
public class RabbitMQController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMsg")
    public String sendMsg(){
        for (int i = 0; i < 500 ; i++) {
            Map<String,String> map = new HashMap<>();
            map.put("content",i+"一德格拉米");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            map.put("sendTime",df.format(new Date()));
            //信息的配置
            MessageProperties messageProperties = new MessageProperties();
            //消息存活时间为2秒
            messageProperties.setExpiration("2000");

//            String msg = i+"lxg";
            //正常需要把map先转成json再转byte
//            Message message = new Message(msg.getBytes(StandardCharsets.UTF_8), messageProperties);
            rabbitTemplate.convertAndSend("my_exchange","wllxy",map);
        }
        return "发送成功";
    }
}
